﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Weixin.Utils;
using Weixin.Entity.Weixin;

namespace Weixin.Web
{
    /// <summary>
    /// 微信接口
    /// </summary>
    public class api : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string signature = context.Request.Params["signature"];
            string timestamp = context.Request.Params["timestamp"];
            string nonce = context.Request.Params["nonce"];
            string echostr = context.Request.Params["echostr"];
            WX wx = new WX(signature, timestamp, nonce, echostr);
            if (!wx.CheckSignature())
            {
                context.Response.Write("err");
                context.Response.End();
            }
            //else
            //{
            //    wx.Valid();
            //}

            if (context.Request.HttpMethod.ToLower() == "post")
            {
                string postStr = "";
                System.IO.Stream s = context.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                postStr = System.Text.Encoding.UTF8.GetString(b);

                if (!string.IsNullOrEmpty(postStr))
                {
                    try
                    {
                        Logger.Log4Net.Info(postStr);
                        WXReceiveData receiveData = wx.GetWeixinMessage(postStr);
                        WXSendData sendData = new WXSendData();
                        sendData.ToUserName = receiveData.FromUserName;
                        sendData.FromUserName = receiveData.ToUserName;
                        sendData.CreateTime = receiveData.CreateTime;

                        if (receiveData.MsgType == "event")
                        {
                            sendData.MsgType = "text";
                            if (receiveData.Event == "subscribe")
                            {
                                sendData.Content = ConfigHelper.GetConfigString("Welcome");
                            }
                            if (receiveData.Event == "click")
                            {

                            }
                            if (receiveData.Event == "view")
                            {

                            }
                        }

                        string rtnData = wx.SendDataMessage(sendData);

                        context.Response.Write(rtnData);
                        context.Response.End();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log4Net.Error(ex.Message + "\r\n" + ex.StackTrace);
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}